package com.cloudsigns.factorymanager2.project.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.project.activity.AddEngineeringInfomartionActivity;
import com.cloudsigns.factorymanager2.project.adapter.EngineeringInformationAdapter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 编辑  批量安装单
 * Created by tony on 2017/12/22.
 */

public class BatchInstallationOrderEditFragment extends Fragment implements View.OnClickListener {
    private ImageView img_edit_engineering_information;
    private ListView lv;
    private TextView btn_save;
    private EngineeringInformationAdapter adapter;
    private List<EngineeringInstallationOrderBean> engineeringBeanList = new ArrayList<>();
    private final String TAG = "BatchInstallationOrderEditFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_batch_installation_order_edit, container, false);
        init(view);
        return view;
    }


    private void init(View view) {
        img_edit_engineering_information = view.findViewById(R.id.img_edit_engineering_information);
        lv = view.findViewById(R.id.lv);
        btn_save = view.findViewById(R.id.btn_save);

        btn_save.setOnClickListener(this);
        img_edit_engineering_information.setOnClickListener(this);

        adapter = new EngineeringInformationAdapter(getActivity(), TAG);
        adapter.setData(engineeringBeanList);
        lv.setAdapter(adapter);
    }

    private void save() {
        Toast.makeText(getActivity(), "保存成功", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {

            case R.id.img_edit_engineering_information:
                mIntent = new Intent(getActivity(), AddEngineeringInfomartionActivity.class);
                startActivityForResult(mIntent, ConstantUtil.ADD_PRODUCT_LIST);
                break;
            case R.id.btn_save:
                save();
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.ADD_PRODUCT_LIST: //添加产品清单
                if (data != null) {
                    boolean bool = data.getBooleanExtra(ConstantUtil.PRODUCTLIST, false);
                    if (bool) {
                        engineeringBeanList.add(new EngineeringInstallationOrderBean());
                        adapter.setData(engineeringBeanList);
                        adapter.notifyDataSetChanged();
                    }

                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}
